//
//  AppleHealthController.swift
//  Alamofire
//
//  Created by 易先生 on 2022/7/22.
//

import UIKit
import HealthKit
import MYModule


class AppleHealthController: BaseViewController {
    
    fileprivate let healthKitManager = HealthKitManager.sharedInstance
    
    fileprivate var steps = [HKQuantitySample]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        requestHealthKitAuthorization()
        
    }
    
    override func makeUI() {
        super.makeUI()
        
        self.navigation.bar.backgroundColor = UIColor.darkBackground
        view.backgroundColor = UIColor.background
        
        view.addSubview(titleView)
        view.addSubview(healthImageView)
        
        titleView.snp.makeConstraints { make in
            make.top.equalTo(topLayoutGuide.snp.bottom)
            make.leading.trailing.equalToSuperview()
        }
        
        healthImageView.snp.makeConstraints { make in
            make.top.equalTo(titleView.snp.bottom).offset(74)
            make.centerX.equalToSuperview()
        }

    }
    
    private lazy var titleView: TopTitleView = {
        let titleView = TopTitleView()
        titleView.updateTitleAndDetailSpec(spec: 16)
        titleView.configTitle(title: "apple_health_title".language,detail: "apple_health_connect_title".language)
        return titleView
    }()
    
    private lazy var healthImageView: ImageView = {
        let healthImageView = ImageView()
        healthImageView.image = UIImage(named: "img_health")
        return healthImageView
    }()

}


private extension AppleHealthController {
    
    func requestHealthKitAuthorization() {
        let writeStes = healthKitManager.writeSets
        let readSets = healthKitManager.readSets
        healthKitManager.healthStore?.requestAuthorization(toShare: writeStes as? Set<HKSampleType>, read: readSets as? Set<HKObjectType>, completion: { (success, error) in
            if success {
                
            } else {
                print(error.debugDescription)
            }
        })
    }
    
    
    func queryStepsSum() {
        let sumOption = HKStatisticsOptions.cumulativeSum
        let statisticsSumQuery = HKStatisticsQuery(quantityType: healthKitManager.stepsCount!, quantitySamplePredicate: nil, options: sumOption) { [unowned self] (query, result, error) in
            if let sumQuantity = result?.sumQuantity() {
                
                let numberOfSteps = Int(sumQuantity.doubleValue(for: self.healthKitManager.stepUnit))
//                print(numberOfSteps)
            }
            
        }
        healthKitManager.healthStore?.execute(statisticsSumQuery)
    }
    
    func querySteps() {
        let sampleQuery = HKSampleQuery(sampleType: healthKitManager.stepsCount!,
                                        predicate: nil,
                                        limit: 100,
                                        sortDescriptors: nil)
        { [unowned self] (query, results, error) in
            if let results = results as? [HKQuantitySample] {
                self.steps = results
            }
        }
        
        healthKitManager.healthStore?.execute(sampleQuery)
    }
}

